package com.finstone.data.spider.eshop;

/**
 * 试验成功
 * Created by TyroneSean on 2016/3/23.
 */
public class Unicode2Str {
    /**
     * 字符串转换unicode
     */
    public static String string2Unicode(String string) {
        StringBuffer unicode = new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            // 取出每一个字符
            char c = string.charAt(i);
            // 转换为unicode
            unicode.append("\\u" + Integer.toHexString(c));
        }
        return unicode.toString();
    }

    /**
     * unicode 转字符串
     */
    public static String unicode2String(String unicode) {
        StringBuffer string = new StringBuffer();
        String[] hex = unicode.split("\\\\u");
        for (int i = 1; i < hex.length; i++) {
            // 转换出每一个代码点
            int data = Integer.parseInt(hex[i], 16);
            // 追加成string
            string.append((char) data);
        }
        return string.toString();
    }
    public static void main(String[] args) {
//        String test = "实时微博";
        String test = "最代码网站地址:www.zuidaima.com";
        String unicode = string2Unicode(test);
        String string = unicode2String(unicode) ;
        System.out.println(unicode);
        System.out.println(string);

        String unicode2 = "{\"pid\":\"pl_common_bottomInput\",\"js\":[\"apps\\/search_v6\\/js\\/pl\\/common\\/bottomInput.js?version=20160322182500\"],\"css\":[],\"html\":\"<div class=\\\"bottom_wrap\\\"> \\n \\t<div class=\\\"bottom_search clearfix\\\">\\n\\t<div class=\\\"search_input\\\">\\n\\t\\t<div class=\\\"search_input_wrap\\\">\\n\\t\\t<div class=\\\"searchBtn_box\\\">\\n\\t<a class=\\\"searchBtn\\\" href=\"http://s.weibo.com/weibo/\\\"\"\\/weibo\\/%2523%25E5%25BE%25AE%25E6%2591%2584%25E5%25BD%25B1%2523\\\" node-type=\\\"submit\\\" suda-data=\\\"key=tblog_search_v4.1&value=nologin_search\\\">\\u641c\\u7d22<\\/a>\\n\\t\\t<\\/div>\\n\\t\\t<div class=\\\"searchInp_box\\\">\\n\\t<div class=\\\"searchInp_auto\\\">\\n\\t<input type=\\\"text\\\" class=\\\"searchInp_form\\\" value=\\\"#\\u5fae\\u6444\\u5f71#\\\" maxlength=\\\"40\\\">\\n\\t<\\/div>\\n\\t\\t<\\/div>\\n\\t\\t<\\/div>\\n\\t<\\/div>\\n\\t<\\/div>\\n  <!-- <div class=\\\"search_feedback\\\">\\n  <p>\\u6b22\\u8fce\\u63d0\\u4ea4\\u5fae\\u535a\\u641c\\u7d22\\u4f7f\\u7528\\u53cd\\u9988\\uff0c\\u8bf7\\u76f4\\u63a5<a href=\"http://s.weibo.com/weibo/\\\"\"javascript:void(0);\\\" action-type=\\\"login\\\">\\u53d1\\u8868\\u610f\\u89c1<\\/a>\\u6216\\u60a8\\u53ef\\u4ee5\\u5173\\u6ce8\\u840c\\u5c0f\\u641c<a href=\"http://s.weibo.com/weibo/\\\"\"http:\\/\\/weibo.com\\/wbsearch\\\" title=\\\"\\u6b22\\u8fce\\u8c03\\u620f\\u6700\\u840c\\u5b98\\u535a\\u5c4c\\u4e1d~~\\\">@\\u5fae\\u535a\\u641c\\u7d22<\\/a>\\u83b7\\u53d6\\u641c\\u7d22\\u6280\\u5de7\\u3002<\\/p>\\n <\\/div> -->\\n<\\/div>\"}";

        System.out.println(unicode2String(unicode2));

    }
}
